/**
 * 
 */
package com.wskj.library.book.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.wskj.library.pojo.BookInfo;
import com.wskj.library.service.BookService;
import com.wskj.library.service.impl.BookServiceImpl;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;


/**
 * @author 胡洋
 * @describe
 * @Date 2018年3月26日下午1:27:48
 * @version 1.0
 */
@MultipartConfig
@WebServlet(urlPatterns = "/book/importExcel.do")
public class BookImportExcelServlet extends HttpServlet {
	BookService  bookService=new BookServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/page/book/importExcel.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<BookInfo> books=new ArrayList<BookInfo>();
		Part book = req.getPart("book");
		
		try (InputStream ins = book.getInputStream();
			Workbook workbook = WorkbookFactory.create(ins);){
			
			Sheet sheet = workbook.getSheetAt(0);

			for (int i = 1; i <=sheet.getLastRowNum(); i++) {
				Row row = sheet.getRow(i);
				String bname=(String) getCellValue(row.getCell(1));
				//分类编号	图片位置	图书的摘要
                String  author=(String) getCellValue(row.getCell(2));
				Date publishTime=(Date) getCellValue(row.getCell(3));
				String  publisher=(String) getCellValue(row.getCell(4));
				double price=(double) getCellValue(row.getCell(5));
				Date collectDay=(Date) getCellValue(row.getCell(6));
                int cid=(int) Double.parseDouble( getCellValue(row.getCell(7)).toString()); // 数值是double 
				String  sumary = (String) getCellValue(row.getCell(8));
				BookInfo bookInfo=new BookInfo(bname,author,publishTime,publisher,price,collectDay,cid,sumary);
			   	books.add(bookInfo);			
			}

		} catch (EncryptedDocumentException | InvalidFormatException e) {
			e.printStackTrace();
		}
		
		
		boolean result=bookService.saveBooks(books);
		
		resp.sendRedirect(req.getContextPath()+"/book/list.do");
		

	}

	/**
	 * 根据单元格的数据类型 获取单元格的值 ......
	 * @param cell 单元格
	 * @return
	 */
	private Object getCellValue(Cell cell) {
       Object obj=null;
		switch (cell.getCellTypeEnum()) {
		case STRING:
			obj=cell.getRichStringCellValue().getString();
			break;
		case NUMERIC:
			if (DateUtil.isCellDateFormatted(cell)) {
				obj=cell.getDateCellValue();
			} else {
				obj=cell.getNumericCellValue();
			}
			break;
		case BOOLEAN:
			obj=cell.getBooleanCellValue();
			break;
		case FORMULA:
			obj=cell.getCellFormula();
			break;
		case BLANK:
			break;			
		default:
			break;
		}
		return obj;
	}

}
